package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {


    /**
     * 插入订单
     * @param orders
     */
    Long insert(Orders orders);

    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    /**
     * 订单分页查询
     * @param ordersPageQueryDTO
     * @return
     */
    Page<Orders> selectByPage(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 根据订单id查询订单详情
     * @param id
     * @return
     */
    @Select("select * from orders where id = #{id}")
    Orders selectByOrderId(Long id);

    /**
     * 改变订单状态
     * @param id
     */
    @Update("update orders set status = #{status} where id = #{id}")
    void statusChange(Long id, Integer status);

    /**
     * 各个状态订单数量统计
     */
    @Select("select count(*) from orders where status = #{status}")
    Integer count(Integer status);

    /**
     * 根据订单id查询地址簿id
     * @param orderId
     * @return
     */
    @Select("select address_book_id from orders where id = #{orderId}")
    Long selectAddressBookId(Long orderId);

    /**
     * 添加拒单原因
     * @param rejectionReason
     */
    @Update("update orders set rejection_reason = #{rejectionReason} where id = #{id}")
    void addRejectionReason(Long id, String rejectionReason);

    /**
     * 添加取消原因
     * @param id
     * @param cancelReason
     */
    @Update("update orders set cancel_reason = #{cancelReason} where id = #{id}")
    void addCancelReson(Long id, String cancelReason);


    /**
     * 根据状态和时间查询订单
     * @param pendingPayment
     * @param time
     */
    @Select("select * from orders where status = #{pendingPayment} and order_time < #{time}")
    List<Orders> selectByStatusAndTimeLT(Integer pendingPayment, LocalDateTime time);

    /**
     * 查询营业额
     * @param map
     * @return
     */
    Double getTurnOver(Map map);

    /**
     * 统计订单数量
     * @param map
     * @return
     */
    Integer countOrders(Map map);
}
